
package br.com.aeternu.controleatendimento.excel.medico;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.apache.log4j.Logger;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import br.com.aeternu.controleatendimento.common.bean.CommonBean;
import br.com.aeternu.controleatendimento.common.ifc.IfcMensagemUsuario;
import br.com.aeternu.controleatendimento.excel.Excel;
import br.com.aeternu.controleatendimento.exception.ControleAtendimentoException;

public class ExcelMedico extends Excel
{

   private static Logger logger = Logger.getLogger(ExcelMedico.class);
   private static final String SHEET = SHEET_MEDICOS;

   public ExcelMedico()
   {
      super();
   }

   @Override
   public void atualiza(CommonBean bean)
   {
      MedicoBean medicoBean = (MedicoBean) bean;

      try
      {
         HSSFWorkbook workbook = obterPastaDeTrabalho();

         HSSFSheet sheet = getSheet(workbook);

         HSSFCellStyle cellStyle = getCellStyleGeral(workbook);
         HSSFRow row;
         HSSFCell cell;

         if (medicoBean.getLinha() > 0)
         {
            row = sheet.getRow(medicoBean.getLinha());
            // Ativo/Inativo
            cell = row.getCell(0);
            cell.setCellValue((medicoBean.isAtivo() ? "SIM" : "NÃO"));
            cell.setCellStyle(cellStyle);
            // Nome
            cell = row.getCell(1);
            cell.setCellValue(medicoBean.getNome());
            cell.setCellStyle(cellStyle);
         }
         else
         {
            row = sheet.createRow(sheet.getLastRowNum() + 1);
            medicoBean.setLinha(sheet.getLastRowNum());
            // Ativo/Inativo
            cell = row.createCell(0);
            cell.setCellValue((medicoBean.isAtivo() ? "SIM" : "NÃO"));
            cell.setCellStyle(cellStyle);
            // Nome
            cell = row.createCell(1);
            cell.setCellValue(medicoBean.getNome());
            cell.setCellStyle(cellStyle);
         }

         gravar(workbook);

         logger.info("Linha " + medicoBean.getLinha() + " atualizada com sucesso!");
      }
      catch (ControleAtendimentoException e)
      {
         logger.error(e.getMessage(), e);
      }
      catch (FileNotFoundException e)
      {
         logger.error(e.getMessage(), e);
      }
      catch (IOException e)
      {
         logger.error(e.getMessage(), e);
      }
   }

   @Override
   public void atualiza(List<? extends CommonBean> beanList)
   {
      @SuppressWarnings("unchecked")
      List<MedicoBean> medicoBeanList = (List<MedicoBean>) beanList;

      for (MedicoBean bean : medicoBeanList)
      {
         atualiza(bean);
      }
   }

   @Override
   public List<MedicoBean> obterRegistros() throws ControleAtendimentoException
   {
      List<MedicoBean> medicoBeanList = new ArrayList<MedicoBean>();

      HSSFSheet sheet = getSheet();

      for (Iterator<Row> rit = (Iterator<Row>) sheet.rowIterator(); rit.hasNext();)
      {
         HSSFRow row = (HSSFRow) rit.next();
         MedicoBean bean = new MedicoBean();

         // pulando o cabeçalho
         if (row.getRowNum() > 0 && row.getCell(0) != null)
         {
            if (row.getRowNum() == 1 && getString(row.getCell(0)) == null)
            {
               // Primeira linha não possui dado válido
               throw new ControleAtendimentoException(IfcMensagemUsuario.ERRO_MEDICO_NAO_ENCONTRADO);
            }

            bean.setLinha(row.getRowNum());
            bean.setAtivo(getBoolean(row.getCell(0)));
            bean.setNome(getString(row.getCell(1)));

            medicoBeanList.add(bean);
         }
      }

      return medicoBeanList;
   }

   @Override
   protected HSSFSheet getSheet() throws ControleAtendimentoException
   {
      return getSheet(obterPastaDeTrabalho());
   }

   @Override
   protected HSSFSheet getSheet(HSSFWorkbook workbook) throws ControleAtendimentoException
   {
      HSSFSheet sheet = workbook.getSheet(SHEET);

      if (sheet == null)
      {
         criarNovaPlanilhaMedicos(workbook, getCellStyleCabecalho(workbook), getCellStyleGeral(workbook));
         workbook = obterPastaDeTrabalho();
         sheet = workbook.getSheet(SHEET);
      }

      return sheet;
   }
}
